package slh.Book;

import slh.ConSql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BookDao {
     private Connection conn;
    public BookDao(Connection conn) {
        this.conn=conn;
    }

    public static List<Book> searchBooks(String keyword) throws SQLException {
        List<Book> books = new ArrayList<>();
        String sql = "SELECT * FROM book WHERE TITLE LIKE ? OR DESCRIPTION LIKE ?";

        try (Connection conn = ConSql.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, "%" + keyword + "%");
            stmt.setString(2, "%" + keyword + "%");

            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    Book book = new Book();
                    book.setBookId(rs.getInt("BOOK_ID"));
                    book.setTitle(rs.getString("TITLE"));
                    book.setAuthor(rs.getString("AUTHOR"));
                    book.setPublisher(rs.getString("PUBLISHER"));
                    book.setPublishYear(rs.getInt("PUBLISH_YEAR"));
                    book.setPages(rs.getInt("PAGES"));
                    book.setLanguage(rs.getString("LANGUAGE"));
                    book.setTranslator(rs.getString("TRANSLATOR"));
                    book.setDescription(rs.getString("DESCRIPTION"));
                    book.setCoverImage(rs.getString("COVER_IMAGE"));
                    book.setScore(rs.getDouble("SCORE"));
                    book.setRatingCount(rs.getInt("RATING_COUNT"));
                    books.add(book);
                }
            }
        }
        return books;
    }

    public static Book getBookById(int id) throws SQLException {
        String sql = "SELECT * FROM book WHERE BOOK_ID = ?";

        try (Connection conn = ConSql.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, id);

            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    Book book = new Book();
                    book.setBookId(rs.getInt("BOOK_ID"));
                    book.setTitle(rs.getString("TITLE"));
                    book.setAuthor(rs.getString("AUTHOR"));
                    book.setPublisher(rs.getString("PUBLISHER"));
                    book.setPublishYear(rs.getInt("PUBLISH_YEAR"));
                    book.setPages(rs.getInt("PAGES"));
                    book.setLanguage(rs.getString("LANGUAGE"));
                    book.setTranslator(rs.getString("TRANSLATOR"));
                    book.setDescription(rs.getString("DESCRIPTION"));
                    book.setCoverImage(rs.getString("COVER_IMAGE"));
                    book.setScore(rs.getDouble("SCORE"));
                    book.setRatingCount(rs.getInt("RATING_COUNT"));
                    return book;
                }
            }
        }
        return null;
    }

    // 新增方法：更新书籍评分
    public static boolean updateBookRating(int bookId, double newScore) throws SQLException {
        String sql = "UPDATE book SET SCORE = ?, RATING_COUNT = RATING_COUNT + 1 WHERE BOOK_ID = ?";

        try (Connection conn = ConSql.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setDouble(1, newScore);
            stmt.setInt(2, bookId);

            int affectedRows = stmt.executeUpdate();
            return affectedRows > 0;
        }
    }
    // 批量查询图书的方法
    public static List<Book> getBooksByIds(List<Integer> ids) throws SQLException {
        if (ids == null || ids.isEmpty()) {
            return Collections.emptyList();
        }

        String sql = "SELECT * FROM book WHERE BOOK_ID IN (" +
                String.join(",", Collections.nCopies(ids.size(), "?")) + ")";

        List<Book> books = new ArrayList<>();
        try (Connection conn = ConSql.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            for (int i = 0; i < ids.size(); i++) {
                stmt.setInt(i + 1, ids.get(i));
            }

            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    Book book = new Book();
                    // 设置book属性...
                    books.add(book);
                }
            }
        }
        return books;
    }
    // 新增方法：获取评分最高的书籍
    public static List<Book> getTopRatedBooks(int limit) throws SQLException {
        List<Book> books = new ArrayList<>();
        String sql = "SELECT * FROM book WHERE RATING_COUNT > 0 ORDER BY SCORE DESC LIMIT ?";

        try (Connection conn = ConSql.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, limit);

            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    Book book = new Book();
                    book.setBookId(rs.getInt("BOOK_ID"));
                    book.setTitle(rs.getString("TITLE"));
                    book.setAuthor(rs.getString("AUTHOR"));
                    book.setPublisher(rs.getString("PUBLISHER"));
                    book.setPublishYear(rs.getInt("PUBLISH_YEAR"));
                    book.setPages(rs.getInt("PAGES"));
                    book.setLanguage(rs.getString("LANGUAGE"));
                    book.setTranslator(rs.getString("TRANSLATOR"));
                    book.setDescription(rs.getString("DESCRIPTION"));
                    book.setCoverImage(rs.getString("COVER_IMAGE"));
                    book.setScore(rs.getDouble("SCORE"));
                    book.setRatingCount(rs.getInt("RATING_COUNT"));
                    books.add(book);
                }
            }
        }
        return books;
    }
}